package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.NoticeEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by pjn on 2017/3/3.
 */
public interface NoticeAddRepository extends CrudRepository<NoticeEntity, Long> {

    public List<NoticeListDto> getNoticesended4DT(DTRequestDto dtRequestDto) throws Exception;

    public Integer getNoticesendedCount4DT(String strSearch) throws Exception;


    @Modifying
    @Query(value = "delete from NoticeEntity n where n.noticeId=?1")
    public Integer deleteNotice(Integer noticeId) throws Exception;

    @Query(value = "select distinct new com.hualude.roadsign.dto.OrgDto4Notice(o.orgId,o.orgName) " +
            "from UserNoticeEntity un, UserEntity u, OrgEntity o " +
            "where u.userId = un.userId and o.orgId = u.orgId and un.noticeId = ?1")
    public List<OrgDto4Notice> getSelectNoticeSendedOrg(Integer noticeId) throws Exception;

    @Query(value = "select new com.hualude.roadsign.dto.OrgDto4Notice(o.orgId,o.orgName,o.orgType) from OrgEntity o")
    public List<OrgDto4Notice> getOrgInfo4Notice() throws Exception;

    public List<NoticeListDto> getNoticeAppSended4DT(String search, DTRequestDto dtRequestDto, Integer userId) throws Exception;

    public Integer getNoticeAppSendedCount4DT(String strSearch, Integer userId) throws Exception;


    //获取app端的轮播公告
    public List<NoticeListDto> getAppPicNotice() throws Exception;

    @Query(value = "SELECT COUNT(*) FROM UserNoticeEntity un,NoticeEntity n WHERE un.userId =?1 AND un.isRead=0 AND un.noticeId= n.noticeId AND n.noticeType=0")
    Integer getIsReadNotice(Integer userId) throws Exception;
}
